package com.example.library.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.example.library.R;
import com.example.library.model.ReservationHistory;
import com.example.library.util.LocalStorageManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ReservationHistoryAdapter extends RecyclerView.Adapter<ReservationHistoryAdapter.ViewHolder> {
    private List<ReservationHistory> historyList;
    private Context context;
    private SimpleDateFormat dateFormat;
    private LocalStorageManager storageManager;

    public ReservationHistoryAdapter(Context context, List<ReservationHistory> historyList) {
        this.context = context;
        this.historyList = historyList;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        this.storageManager = new LocalStorageManager(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_reservation_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ReservationHistory history = historyList.get(position);
        
        holder.tvType.setText(history.getType());
        holder.tvSeatNumber.setText(history.getSeatNumber());
        holder.tvStatus.setText(history.getStatus());
        holder.tvTime.setText(dateFormat.format(new Date(history.getTimestamp())));

        // 设置状态的背景颜色
        int backgroundColor;
        if ("已预约".equals(history.getStatus())) {
            backgroundColor = ContextCompat.getColor(context, R.color.reserved);
        } else if ("已取消".equals(history.getStatus())) {
            backgroundColor = ContextCompat.getColor(context, R.color.themeGray);
        } else {
            backgroundColor = ContextCompat.getColor(context, R.color.available);
        }
        holder.tvStatus.setBackgroundColor(backgroundColor);

        // 设置删除按钮点击事件
        holder.btnDelete.setOnClickListener(v -> showDeleteConfirmDialog(history));
    }

    @Override
    public int getItemCount() {
        return historyList.size();
    }

    public void updateData(List<ReservationHistory> newHistoryList) {
        this.historyList = newHistoryList;
        notifyDataSetChanged();
    }

    private void showDeleteConfirmDialog(ReservationHistory history) {
        new AlertDialog.Builder(context)
                .setTitle("删除确认")
                .setMessage("确定要删除这条预约记录吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    // 删除记录
                    storageManager.deleteReservationHistory(history.getUserId(), history.getTimestamp());
                    // 更新列表
                    historyList.remove(history);
                    notifyDataSetChanged();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvType;
        TextView tvSeatNumber;
        TextView tvStatus;
        TextView tvTime;
        ImageButton btnDelete;

        ViewHolder(View itemView) {
            super(itemView);
            tvType = itemView.findViewById(R.id.tvType);
            tvSeatNumber = itemView.findViewById(R.id.tvSeatNumber);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            tvTime = itemView.findViewById(R.id.tvTime);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }
} 